package com.rysima.wujiansima.demo.demo1;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/*
分析以下需求，并用代码实现：

(1)统计每个单词出现的次数

(2)有如下字符串"If you want to change your fate I think you must come to the dark horse to learn java"(用空格间隔)

(3)打印格式：

    to=3

  	think=1

  	you=2

 */
public class HomeWork2 {
    public static void main(String[] args) {
        String s = "If you want to change your fate I think you must come to the dark horse to learn java";
        String[] s1 = s.split(" ");
        Map<String,Integer> map = new HashMap<>();
//        for (String s2 : s1) {
//            if (map.containsKey(s2)){
//                Integer integer = map.get(s2);
//                integer++;
//                map.put(s2,integer);
//
//            }else {
//                map.put(s2,1);
//            }
//        }
        for (int i = 0; i < s1.length; i++) {

            if (map.containsKey(s1[i])){
                Integer integer = map.get(s1[i]);
                integer++;
                map.put(s1[i],integer);
            }else{
                map.put(s1[i],1);
            }
        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        for (Map.Entry<String, Integer> entry : entries) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            System.out.println(key + "=" + value);
        }
    }
}
